package com.uminho.tools;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import android.content.Context;
import android.util.Log;

import com.uminho.objects.Appointment;

public class Disponibilidades {
	private static String busyDaysfile = "serviceBusyDays";
	public static HashMap<Date, Integer> busyDays;
	public static HashMap<Date, Integer> serviceBusyDays;
	Disponibilidades(){
	}
	public static void derementBusyDays(Date d){
		try{
		int i = (int)busyDays.get(d)*24/100;
		i=i-1;
		if(i>0) busyDays.put(d, new Integer(i*100/24));
		else busyDays.remove(d);
		}catch(Exception e){e.printStackTrace();}
	}
	public static void derementserviceBusyDays(Date d){
		try{
		int i = (int)serviceBusyDays.get(d);
		i=i-1;
		if(i>0) serviceBusyDays.put(d, new Integer(i));
		else serviceBusyDays.remove(d);
		}catch(Exception e){e.printStackTrace();}
	}
	
	public static boolean saveBusyDays(){
		try{
		Context c = ApplicationContext.c;
		FileOutputStream fos = null;
		ObjectOutputStream outputStream = null;
		try {
			fos = c.openFileOutput(busyDaysfile, Context.MODE_PRIVATE);
			outputStream = new ObjectOutputStream(fos);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		try {
			//Iterator it = ap.iterator();
			//while(it.hasNext()){
				outputStream.writeObject(busyDays);
			//}
			
			if (outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

		}catch(Exception e){e.printStackTrace();return true;}
	}
	
	public static boolean openBusyDays() {
		try{
		busyDays=new HashMap<Date, Integer>();
		Context c = ApplicationContext.c;
        ObjectInputStream inputStream = null;
        FileInputStream fis = null;
		try {
			fis = c.openFileInput(busyDaysfile);
			 //Construct the ObjectInputStream object
            inputStream = new ObjectInputStream(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
        try {
            
           
            Object obj = null;
            obj = inputStream.readObject();
            busyDays=(HashMap<Date, Integer>) obj;
           
            
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (EOFException eof) {
            //eof.printStackTrace();
            Log.d("aqui", eof.toString());
            try {
				inputStream.close();
				 fis.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return false;
        
        }catch (Exception ex) {
            ex.printStackTrace();
        }
            //Close the ObjectInputStream
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if(fis != null){
                	fis.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                
            }
        
        return true;
		}catch(Exception e){e.printStackTrace();return true;}
    }
}
